package com.example.day01_homework3;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recl;
    private SmartRefreshLayout smart;
    private HomeAdapter homeAdapter;
    private int page=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        recl = (RecyclerView) findViewById(R.id.recl);
        smart = (SmartRefreshLayout) findViewById(R.id.smart);
        recl.setLayoutManager(new LinearLayoutManager(this));

        homeAdapter = new HomeAdapter(this);
        recl.setAdapter(homeAdapter);
        initData();
        smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page=1;
                initData();
            }
        });
    }

    private void initData() {

        OkHttpClient okHttpClient = new OkHttpClient();
        Request build = new Request.Builder().get().url("https://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page="+page).build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                Gson gson = new Gson();
                final ArrayList<Bean.DataBean> data = (ArrayList<Bean.DataBean>) gson.fromJson(json, Bean.class).getData();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(page==1){
                            homeAdapter.setRefresh(data);
                            smart.finishRefresh();
                        }else {
                            homeAdapter.setLoadMore(data);
                            smart.finishLoadMore();
                        }
                    }
                });
            }
        });
    }
}
